#!/bin/sh

PYTHON="$1"
SUBLIME="$2"

# We need to remove the stupid "file://" that Evince puts in front of the file name
# Notice the wild quoting: we need to interpolate $SUBLIME
# In the end, EDITORCMD should be of the form:
#
# f=%f; sublime-text "${f#file://}:%l"
#
# where sublime-text is the name of the sublime-text binary
EDITORCMD="f=\"%f\"; $SUBLIME \"\${f#file://}:%l\""

PDFFILE="$3"

if [ -f "$PDFFILE" ];then
	# danke an Feuerfieber
	# http://forum.ubuntuusers.de/topic/evince-synctex-vim-emacs-scite-lyx-kile-editor/#post-2841828
	if [ -f "${PDFFILE%%.pdf}.synctex.gz" ];then
		# we change the cwd to this directory, so this should work
		# also note that we stick in 'python' because the script file need not be executable
		$PYTHON evince_backward_search "$PDFFILE" "$EDITORCMD"&
		BACKWARD_SEARCH_PID=$!
		echo $BACKWARD_SEARCH_PID
	fi
fi

/usr/bin/evince "$3"

if [ "$BACKWARD_SEARCH_PID" ];then
	echo "Killing $BACKWARD_SEARCH_PID"
	kill $BACKWARD_SEARCH_PID
fi
